﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Top.PointStore.Business
{
    public class IOHelper
    {


        public static string sizeList = "60_60,100_100,190_190,230_230,300_300,350_350,800_800";
        /// <summary>XML反序列化
        /// </summary>
        /// <param name="type">目标类型(Type类型)</param>
        /// <param name="filePath">XML文件路径</param>
        /// <returns>序列对象</returns>
        public static object DeserializeFromXML(Type type, string filePath)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(type);
                return serializer.Deserialize(fs);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }


        /// <summary>
        /// XML序列化
        /// </summary>
        /// <param name="obj">序列对象</param>
        /// <param name="filePath">XML文件路径</param>
        /// <returns>是否成功</returns>
        public static bool SerializeToXml(object obj, string filePath)
        {
            bool result = false;

            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(fs, obj);
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
            return result;

        }

        /// <summary>获得文件物理路径
        /// </summary>
        /// <returns></returns>
        public static string GetMapPath(string path)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(path);
            }
            else
            {
                return System.Web.Hosting.HostingEnvironment.MapPath(path);
            }
        }

        //public static List<Thumb> GetThumbList(string picUrl)
        //{
        //    if (string.IsNullOrEmpty(picUrl)) return null;
        //    var path = picUrl.Substring(0, picUrl.LastIndexOf('/'));
        //    var productPicName = picUrl.Substring(picUrl.LastIndexOf('/'));
        //    var list = new List<Thumb>();
        //    foreach (var size in sizeList.Split(','))
        //    {
        //        string thumPath = path + "/thum" + size;
        //        var picThumbUrl = thumPath + productPicName;
        //        if (string.IsNullOrEmpty(size)) continue;
        //        if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + thumPath))
        //            continue;
        //        if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + picThumbUrl))
        //            continue;
        //        list.Add(new Thumb() { Size = size, PicThumbUrl = picThumbUrl });
        //    }
        //    return list;
        //}


    }
}
